package com.thinkgem.jeesite.modules.sys.utils;

import com.google.common.base.Strings;
import com.thinkgem.jeesite.modules.sys.entity.Menu;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * User: dcai@edianlicai.com
 * Date: 2016/5/19
 * Time: 14:02
 */
public class NavUI {

    public static final String TOP_MENU_ID = "1";
    public static final String SHOW_MENU = "1";

    public static String renderNavi(String ctx) {
        StringBuffer menuBuffer = new StringBuffer();
        final List<Menu> menuList = UserUtils.getMenuList();
        final List<Menu> topMenuList = getSubMenuList(menuList, TOP_MENU_ID);

        for (Menu menu : topMenuList) {
            List<Menu> subMenuList = getSubMenuList(menuList, menu.getId());
            String icon = menu.getIcon() == null ? "" : menu.getIcon();
            String href = Strings.isNullOrEmpty(menu.getHref()) ? "" : (ctx + menu.getHref());

            if (subMenuList.isEmpty()) {

                menuBuffer.append("<li><a href=\"" + href + "\"><i class=\" menu-icon fa " + icon + "\"></i> <span class='menu-text'>&nbsp;" + menu.getName() + "</span></a></li>");
            } else {
                menuBuffer.append("<li>");
                menuBuffer.append("<a href=\"" + href + "\" class=\"dropdown-toggle\" >");
                menuBuffer.append("<i class=\"menu-icon  fa " + icon + "\"></i> <span class='menu-text'>&nbsp;" + menu.getName() + "</span>");
                menuBuffer.append("<b class=\"arrow fa fa-angle-down\"></b>");
                menuBuffer.append("</a>");
                menuBuffer.append("<b class=\"arrow\"></b>");
                menuBuffer.append(renderSubMenu(menu, menuList, ctx));
                menuBuffer.append("</li>");
            }
        }


        return menuBuffer.toString();
    }

    private static String renderSubMenu(Menu menu, List<Menu> menuList, String ctx) {
        final List<Menu> subMenuList = getSubMenuList(menuList, menu.getId());

        StringBuffer mb = new StringBuffer();
        mb.append("<ul class=\"submenu nav-hide\" style=\"display: none;\">");
        for (Menu sub : subMenuList) {
            List<Menu> ssubMenuList = getSubMenuList(menuList, sub.getId());
            String icon = sub.getIcon() == null ? "#" : sub.getIcon();
            String href = Strings.isNullOrEmpty(sub.getHref()) ? "" : (ctx + sub.getHref());
            if (ssubMenuList.isEmpty()) {
                mb.append("<li>");
                mb.append("<a class=\"J_menuItem\" href=\"" + href + "\" >");
                mb.append("<i class=\"fa " + icon + "\" ></i>");
                mb.append("<span class='menu-text'>&nbsp;" + sub.getName() + "</span>");
                mb.append("</a>");
                mb.append("<b class=\"arrow\"></b>");
                mb.append("</li>");

            } else {
                mb.append("<li>");
                mb.append("<a href=\"" + href + "\"  class=\"dropdown-toggle\" >");
                mb.append("<i class=\"fa " + icon + "\"></i> <span class='menu-text'>&nbsp;" + sub.getName() + "</span>");
                mb.append("<b class=\"arrow fa fa-angle-down\"></b>");
                mb.append("</a>");
                mb.append("<b class=\"arrow\"></b>");
                mb.append(renderSubMenu(sub, menuList, ctx));
                mb.append("</li>");
            }
        }
        mb.append("</ul>");
        return mb.toString();
    }

    private static List<Menu> getSubMenuList(List<Menu> menuList, String parentId) {
        if (menuList == null || menuList.isEmpty()) {
            return Collections.emptyList();
        }
        List<Menu> subMenuList = new ArrayList<Menu>();

        for (Menu menu : menuList) {
            if (parentId.equals(menu.getParentId()) && SHOW_MENU.equals(menu.getIsShow())) {
                subMenuList.add(menu);
            }
        }

        return subMenuList;
    }

}
